package tools;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.RollbackException;
import javax.transaction.UserTransaction;
public class Jdbc_Connect_Oracle {
	private int port;
	private String service;
	private String username;
	private String password;
	private String ip;
	private Connection conn;
	private Statement stmt;
	private UserTransaction transaction;
	public int getPort() {
		return port;
	}
	public void setPort(int port) {
		this.port = port;
	}
	public String getService() {
		return service;
	}
	public void setService(String service) {
		this.service = service;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getIp() {
		return ip;
	}
	public void setIp(String ip) {
		this.ip = ip;
	}
	public UserTransaction getTransaction() {
		return transaction;
	}
	public void setTransaction(UserTransaction transaction) {
		this.transaction = transaction;
	}
	public Connection getConn() {
		return conn;
	}
	public void setConn(Connection conn) {
		this.conn = conn;
	}
	public Statement getStmt() {
		return stmt;
	}
	public void setStmt(Statement stmt) {
		this.stmt = stmt;
	}
	public void Play_Transaction(List<String>Sql_List) throws SQLException, SecurityException, IllegalStateException, RollbackException, HeuristicMixedException, HeuristicRollbackException
	{
		try
		{
		this.conn.setAutoCommit(false);
		for(Integer i=0;i<Sql_List.size();i++)
		{
			this.stmt.executeUpdate(Sql_List.get(i));
			System.out.println("执行了一条Sql="+Sql_List.get(i));
		}
		this.conn.commit();
		this.conn.setAutoCommit(true);
		System.out.println("事物执行完毕!");
		}
		catch(Exception exc)
		{
			this.conn.rollback();//回滚JDBC事务
			System.out.println("发生Sql异常，完成回滚");
			exc.printStackTrace();    

		}
	}
	
	
	public Jdbc_Connect_Oracle()
	{	
		Read_Properties read_properties=new Read_Properties("db_cn.properties");
		this. port=Integer.valueOf(read_properties.Read_Properties_root("port"));
		this. service=read_properties.Read_Properties_root("service");
		this. username=read_properties.Read_Properties_root("username");
		this. password=read_properties.Read_Properties_root("password");
		this. ip=read_properties.Read_Properties_root("ip");
		System.out.println("IP="+this.ip+" ServicName="+this.service+" Port="+this.port+" Username="+this.username+" Passwor="+this.password);
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
			String url="jdbc:oracle:thin:@"+ip+":"+port+":"+service;
			this.conn=DriverManager.getConnection(url,username,password);
			this.stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_UPDATABLE);
			System.out.println("URL="+url);
			
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	// 执行一组事物操作
	
	
	
	
	
	// 为了控制负载均衡数据库链接字符串
	public Jdbc_Connect_Oracle(boolean load_balance)
	{
		if(load_balance==false)
		{
			Read_Properties read_properties=new Read_Properties("system.properties");
			this. port=Integer.valueOf(read_properties.Read_Properties_root("port"));
			this. service=read_properties.Read_Properties_root("service");
			this. username=read_properties.Read_Properties_root("username");
			this. password=read_properties.Read_Properties_root("password");
			this. ip=read_properties.Read_Properties_root("ip");
			System.out.println("IP="+this.ip+" ServicName="+this.service+" Port="+this.port+" Username="+this.username+" Passwor="+this.password);
			try {
				Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
				String url="jdbc:oracle:thin:@"+ip+":"+port+":"+service;
				this.conn=DriverManager.getConnection(url,username,password);
				this.stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
	                    ResultSet.CONCUR_UPDATABLE);
				System.out.println("URL="+url);
			} catch (InstantiationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else
		{
			Read_Properties read_properties=new Read_Properties("system.properties");
			this. port=Integer.valueOf(read_properties.Read_Properties_root("port"));
			this. service=read_properties.Read_Properties_root("service");
			this. username=read_properties.Read_Properties_root("username");
			this. password=read_properties.Read_Properties_root("password");
			this. ip=read_properties.Read_Properties_root("ip");
			String[]sip=ip.split(";");
			List<String>slist=new ArrayList<String>();
			for(Integer i=0;i<sip.length;i++)
			{
				slist.add(sip[i]);
			}
			try {
				String sips="";
				for(Integer i=0;i<slist.size();i++)
				{
					sips=slist.get(i)+","+sips;	
				}
				this.ip=sips;
				System.out.println("IP="+this.ip+" ServicName="+this.service+" Port="+this.port+" Username="+this.username+" Passwor="+this.password);
				Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
				String url="jdbc:oracle:thin:@(description=(address_list=";
				for(Integer i=0;i<slist.size();i++)
				{
					url=url+"(address=(host="+slist.get(i)+") (protocol=tcp)(port="+this.port+"))";
				}
				url=url+"(load_balance=yes)(failover=yes))(connect_data=(service_name= "+this.service+")))";
				System.out.println("URL="+url);
				this.conn=DriverManager.getConnection(url,username,password);
				this.stmt=conn.createStatement();
			} catch (InstantiationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}catch(SQLException e)
			{
				e.printStackTrace();
			}
		}
	}
	
	
	public Jdbc_Connect_Oracle(int port,String service,String username,String password,List<String> ip)throws SQLException
	{
		try {
			String sip="";
			for(Integer i=0;i<ip.size();i++)
			{
				sip=ip.get(0)+","+sip;	
			}
			this.ip=sip;
			this.password=password;
			this.username=username;
			this.service=service;
			this.port=port;
			System.out.println("IP="+this.ip+" ServicName="+this.service+" Port="+this.port+" Username="+this.username+" Passwor="+this.password);
			Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
			String url="jdbc:oracle:thin:@(description=(address_list=";
			for(Integer i=0;i<ip.size();i++)
			{
				url=url+"(address=(host="+ip.get(i)+") (protocol=tcp)(port="+this.port+"))";
			}
			url=url+"(load_balance=yes)(failover=yes))(connect_data=(service_name= "+this.service+")))";
			System.out.println("URL="+url);
			this.conn=DriverManager.getConnection(url,username,password);
			this.stmt=conn.createStatement();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	
	
	
	public Jdbc_Connect_Oracle(int port,String service,String username,String password,String ip) throws SQLException
	{
		try {
			this.ip=ip;
			this.password=password;
			this.username=username;
			this.service=service;
			this.port=port;
			System.out.println("IP="+this.ip+" ServicName="+this.service+" Port="+this.port+" Username="+this.username+" Passwor="+this.password);
			Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
			String url="jdbc:oracle:thin:@"+ip+":"+port+":"+service;
			System.out.println("URL="+url);
			this.conn=DriverManager.getConnection(url,username,password);
			this.stmt=conn.createStatement();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
